//
//  Api.swift
//  weile
//
//  Created by LKM on 16/10/3.
//  Copyright © 2016年 LKM. All rights reserved.
//

import Foundation
//import SwiftTask
import Alamofire
import SwiftyJSON

/**
 这里就先这样咯。等下好任务管理再来修改这边的写法
 
 这里主要的全局任务就是有登录了。其他暂时没  有的话也就先指定编程咯。
 */

//public class AsyncTask<Progress, Value, Error>:Task<Progress, Value, Error>{
//
//
//    public override init(weakified: Bool, paused: Bool, initClosure: @escaping InitClosure) {
//        super.init(weakified: weakified, paused: paused, initClosure: initClosure);
//    }
//}
//登录后要执行的
class LoginAfterRunType :NSObject{
    
    var run: (Bool)->();
    init(run: @escaping (Bool)->())  {
        self.run=run;
    }
    
    func run (isSuccess: Bool)->(){
        run(isSuccess);
    }
}
//.responseString { (re: DataResponse<String>) in
//    LKMLog(re.result.value);
//}

//typealias LoginAfterRunType = (Bool)->();
class Api{
    
    class func encrypt(str:String) -> String{
        return SecurityUtil.encryptAESData(str, "xurongting666rob");
    
    }
    /**
     把参数进行加密
     */
    class func encrypt(_ pa: Dictionary<String,String>,_ key:String="xurongting666rob") -> String{
        var paa = pa;
        
        paa["sing_time"]=String(ServerTimeSync.getInstance().getServerCurTime())
        
        //利用自带的json库转换成Data
        //如果设置options为JSONSerialization.WritingOptions.prettyPrinted，则打印格式更好阅读
        let data = try? JSONSerialization.data(withJSONObject: paa, options: [])
        //Data转换成String打印输出
        let str = String(data:data!, encoding: String.Encoding.utf8)
       
        return SecurityUtil.encryptAESData(str, key);
        
    }
    
    class func encryptOfUser(_ pa: Dictionary<String,String>)->String{
        return encrypt(pa, UserInfoCache.getInstance().getPwdSign());
        
    }
    
    
//    class func decrypt(){
//        SecurityUtil.decryptAESData("","key");
//    }

    static let host="http://api.vlhome.com.cn";//"http://120.77.8.217:1024";

    //运行中的
//    static var LoginAfterRun=Dictionary<Int, WeakReference<LoginAfterRunType>>();//很快就回收了
    
    //_________________记得每个请求都要_______
    //因为只会有一个界面在监听.所以任务只做单对的 可以是taskser
    static var LoginAfterRun=Dictionary<Int, LoginAfterRunType>();
    //请求中的请求。key是taskSer
    static var RequestIng=Dictionary<Int,DataRequest>();
    
    //________________________
    class func getArticleBaseUrl()-> String {
        return getAPi();
    }
    
    class func getAPi() -> String{
        return host + "/Tools/Api/api.ashx";
    }
    class func getAPiPwd() -> String{
        return host + "/Tools/Api/apipwd.ashx";
    }
    class func  getImageUrl(_ uri:String) ->String{
        return host + uri;
    }
    
    
    //没有的任务序列号
    static let NilTaskSerialNumber=0;
    fileprivate static var taskSerialNumber:Int=0;
    
    //任务序列号
    public class func NextTaskSerialNumber()->Int{
        taskSerialNumber = taskSerialNumber+1;
        return taskSerialNumber;
    }
    
    //销毁
    class func destroyTask(taskSerialNumbers:[Int]){
        for ts in taskSerialNumbers {
            RequestIng[ts]?.cancel();
            
            LoginAfterRun.removeValue(forKey: ts);
            
        }

    }
    @discardableResult
    class func getHelpDetail(preTaskSerialNumber : Int?,id:String,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    getHelpDetail(preTaskSerialNumber:taskSerialNumber,id:id ,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }
        let request = Alamofire
            .request(getAPi(), method: .post,
                     parameters:[
                        "action": "consshow",
                        "id":id
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:false ){ (data :JSON)->Any in
                    return ArticleDetailTo.init(jsons: data.dictionary);
                }
                
                //状态逻辑处理
                //重新登录
                if(re.res==ResponEntity.Code_Autho_Fail){
                    login(nil);
                    //然后重新运行下 监听下登录情况
                    getHelpDetail(preTaskSerialNumber:taskSerialNumber ,id:id,end: end);
                }else{
                    end(re);
                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }
    

    @discardableResult
    class func getHelps(preTaskSerialNumber : Int?,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    getHelps(preTaskSerialNumber:taskSerialNumber ,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }
        
        let request = Alamofire
            .request(getAPi(), method: .post,
                     parameters:[
                        "action":"helplist",
                        "cid":"597"
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:false ){ (data :JSON)->Any in
                    return HelpsTo.initToList(json: data)//(jsons: data.dictionary);
                }
                
                //状态逻辑处理
                //重新登录
                if(re.res==ResponEntity.Code_Autho_Fail){
                    login(nil);
                    //然后重新运行下 监听下登录情况
                    getHelps(preTaskSerialNumber:taskSerialNumber,end:end);
                }else{
                    end(re);
                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }
    @discardableResult
    class func post_xgtoken(preTaskSerialNumber : Int?,deviceId:String,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
//        if isLoging {
//            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
//                if(isSuccess){
//                    post_xgtoken(preTaskSerialNumber:taskSerialNumber ,deviceId:deviceId,end: end);
//                }else{
//                    end(ResponEntity.Fail());
//                }
//            }
//            return taskSerialNumber;
//        }
        
        let request = Alamofire
            .request(getAPiPwd(), method: .post,
                     parameters:[
                        "c":encryptOfUser([
                            "action": "pushlogin",
                            "tid":deviceId
                            ])
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:true ){ (data :JSON)->Any in
                    return "";//ProductsTo.init(dict: data.dictionaryObject!);
                }
                
                //状态逻辑处理
                //重新登录
//                if(re.res==ResponEntity.Code_Autho_Fail){
//                    login(nil);
//                    //然后重新运行下 监听下登录情况
//                    post_xgtoken(preTaskSerialNumber:taskSerialNumber,deviceId:deviceId,end:end);
//                }else{
                    end(re);
//                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }

    
    @discardableResult
    class func getArticleShare(preTaskSerialNumber : Int?,id:String ,end: @escaping (ResponEntity)->() )->Int{
        return getShareConetnt(preTaskSerialNumber: preTaskSerialNumber, kindid: "1", id: id, end: end)
    }

    @discardableResult
    class func getProductShare(preTaskSerialNumber : Int?,id:String ,end: @escaping (ResponEntity)->() )->Int{
        return getShareConetnt(preTaskSerialNumber: preTaskSerialNumber, kindid: "2", id: id, end: end)
    }
    
    @discardableResult
    class func getShareConetnt(preTaskSerialNumber : Int?,kindid: String,id:String ,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    getProductShare(preTaskSerialNumber:taskSerialNumber ,id:id,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }
        
        let request = Alamofire
            .request(getAPi(), method: .post,
                     parameters:[
                        "kindid":kindid,
                        "conid":id,
                        "action":"shareurl"
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:true ){ (data :JSON)->Any in
                    return ProductShareTo.init(jsons: data.dictionary);
                }
                
                //状态逻辑处理
                //重新登录
                if(re.res==ResponEntity.Code_Autho_Fail){
                    login(nil);
                    //然后重新运行下 监听下登录情况
                    getProductShare(preTaskSerialNumber:taskSerialNumber,id:id,end:end);
                }else{
                    end(re);
                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }
    
    @discardableResult
    class func getSearchNew(preTaskSerialNumber : Int?,page:Int,keyWord:String,end: @escaping (ResponEntity)->() )->Int{
        
        if( StringHelp.isEmpty(keyWord) ){
            end(ResponEntity.init(dict: JSON(data:StringHelp.toData("{\"res\":1,\"msg\":\"获取数据成功\",\"data\":[]}")!), isDataNotNil: false, data: { (j:JSON) -> Any in
                return [SearchNewTo]();
            }) );
            
            return 0;
        }
        
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    getSearchNew(preTaskSerialNumber:taskSerialNumber ,page:page,keyWord:keyWord,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }
        //http://120.77.8.217:1024/Tools/Api/api.ashx?action=page_prolist
        let request = Alamofire
            .request(getAPi(), method: .post,
                     parameters:[
                        "action": "seachlist",
                        "currentpage":page,
                        "kindid":"1",//1就是新闻得
                        "keywords":keyWord
                        
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:false ){ (data :JSON)->Any in
                    return SearchNewTo.initToList(json: data)//(dict: data.dictionaryObject!);
                }
                
                //状态逻辑处理
                //重新登录
                if(re.res==ResponEntity.Code_Autho_Fail){
                    login(nil);
                    //然后重新运行下 监听下登录情况
                    getSearchNew(preTaskSerialNumber:taskSerialNumber ,page:page,keyWord:keyWord,end: end);
                }else{
                    end(re);
                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }
    @discardableResult
    class func CollectArticle(preTaskSerialNumber : Int?,id:String ,end: @escaping (ResponEntity)->() )->Int{
        return CollectDD(preTaskSerialNumber:preTaskSerialNumber , kindid: "1", id: id, end: end)
    }
    class func CollectProduct(preTaskSerialNumber : Int?,id:String ,end: @escaping (ResponEntity)->() )->Int{
        return CollectDD(preTaskSerialNumber:preTaskSerialNumber , kindid: "2", id: id, end: end)
    }
    @discardableResult
    class func CollectDD(preTaskSerialNumber : Int?,kindid:String,id:String ,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    CollectDD(preTaskSerialNumber:taskSerialNumber ,kindid:kindid,id:id,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }
        
        let request = Alamofire
            .request(getAPiPwd(), method: .post,
                     parameters:[
                        "c":encryptOfUser([
                            "action": "mykeepsave",
                            "kindid":kindid,
                            "conid":id
                            ])
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:true ){ (data :JSON)->Any in
                    return data.dictionaryObject ?? nil//ProductsTo.init(dict: data.dictionaryObject!);
                }
                
                //状态逻辑处理
                //重新登录
                if(re.res==ResponEntity.Code_Autho_Fail){
                    login(nil);
                    //然后重新运行下 监听下登录情况
                    CollectDD(preTaskSerialNumber:taskSerialNumber,kindid:kindid,id:id,end:end);
                }else{
                    end(re);
                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }
    
    class func MyCollectArticle(preTaskSerialNumber : Int?,page:Int ,end: @escaping (ResponEntity)->() )->Int{
        
        return MyCollects(preTaskSerialNumber: preTaskSerialNumber,kindid:"1", page: page, end: end)
    }
    class func MyCollectProduct(preTaskSerialNumber : Int?,page:Int ,end: @escaping (ResponEntity)->() )->Int{
     
        return MyCollects(preTaskSerialNumber: preTaskSerialNumber,kindid:"2", page: page, end: end)
    }
    /**
     preTaskSerialNumber 外部调用直接nil
     */
    @discardableResult
    class func MyCollects(preTaskSerialNumber : Int?,kindid :String,page:Int ,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    MyCollects(preTaskSerialNumber:taskSerialNumber ,kindid:kindid,page:page,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }
      
        let request = Alamofire
            .request(getAPiPwd(), method: .post,
                     parameters:[
                        "c":encryptOfUser([
                            "action": "mykeep",
                            "kindid":kindid,
                            "currentpage":String(page),
                            "pagesize":String(LKMAPIListPageSize)
                            ])
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:false ){ (data :JSON)->Any in
                    return MyCollectsItemTo.initToList(json: data)//(jsons: data.dictionary);
                }
                
                //状态逻辑处理
                //重新登录
                if(re.res==ResponEntity.Code_Autho_Fail){
                    login(nil);
                    //然后重新运行下 监听下登录情况
                    MyCollects(preTaskSerialNumber:taskSerialNumber,kindid:kindid,page:page,end:end);
                }else{
                    end(re);
                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }
    
    
    @discardableResult
    class func feedbacksave(preTaskSerialNumber : Int?,content:String,phone:String,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    feedbacksave(preTaskSerialNumber:taskSerialNumber,content:content,phone: phone ,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }
        
        let request = Alamofire
            .request(getAPiPwd(), method: .post,
                     parameters:[
                        "c":encryptOfUser([
                            "action":"feedbacksave",
                            "cons":content,
                            "contacts":phone
                            ])
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:false ){ (data :JSON)->Any in
                    return ""//MyInfoTo.init(dict: data.dictionaryObject!);
                }
                
                //状态逻辑处理
                //重新登录
                if(re.res==ResponEntity.Code_Autho_Fail){
                    login(nil);
                    //然后重新运行下 监听下登录情况
                    feedbacksave(preTaskSerialNumber:taskSerialNumber ,content:content,phone: phone,end: end);
                }else{
                    end(re);
                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }

    
    @discardableResult
    class func EditUserInfo(preTaskSerialNumber : Int?,pa :Dictionary<String,String>,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    EditUserInfo(preTaskSerialNumber:taskSerialNumber ,pa:pa,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }
        var paa=pa;
        paa["action"] = "usereditsave";
        let request = Alamofire
            .request(getAPiPwd(), method: .post,
                     parameters:[
                        "c":encryptOfUser(paa)
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:false ){ (data :JSON)->Any in
                    return ""//MyInfoTo.init(dict: data.dictionaryObject!);
                }
                
                //状态逻辑处理
                //重新登录
                if(re.res==ResponEntity.Code_Autho_Fail){
                    login(nil);
                    //然后重新运行下 监听下登录情况
                    EditUserInfo(preTaskSerialNumber:taskSerialNumber ,pa:pa,end: end);
                }else{
                    end(re);
                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }
    
    @discardableResult
    class func getUserInfo(preTaskSerialNumber : Int?,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    getUserInfo(preTaskSerialNumber:taskSerialNumber ,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }
        let request = Alamofire
            .request(getAPiPwd(), method: .post,
                     parameters:[
                        "c":encryptOfUser([
                            "action": "userinfo"
                            ])
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:true ){ (data :JSON)->Any in
                    return MyInfoTo.init(jsons: data.dictionary);
                }
                
                //状态逻辑处理
                //重新登录
                if(re.res==ResponEntity.Code_Autho_Fail){
                    login(nil);
                    //然后重新运行下 监听下登录情况
                    getUserInfo(preTaskSerialNumber:taskSerialNumber ,end: end);
                }else{
                    end(re);
                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }

    
    @discardableResult
    class func getSysInfo(preTaskSerialNumber : Int?,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    getSysInfo(preTaskSerialNumber:taskSerialNumber ,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }
        let request = Alamofire
            .request(getAPi(), method: .post,
                     parameters:[
                        "action": "model_sys"
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:true ){ (data :JSON)->Any in
                    return ContactServiceTo.init(jsons: data.dictionary);
                }
                
                //状态逻辑处理
                //重新登录
                if(re.res==ResponEntity.Code_Autho_Fail){
                    login(nil);
                    //然后重新运行下 监听下登录情况
                    getSysInfo(preTaskSerialNumber:taskSerialNumber ,end: end);
                }else{
                    end(re);
                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }
    
    @discardableResult
    class func getMyCenter(preTaskSerialNumber : Int?,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    getMyCenter(preTaskSerialNumber:taskSerialNumber ,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }
        let request = Alamofire
            .request(getAPi(), method: .post,
                     parameters:[
                        "action": "page_usergroup"
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:true ){ (data :JSON)->Any in
                    return MyCenterTo.init(jsons: data.dictionary);
                }
                
                //状态逻辑处理
                //重新登录
                if(re.res==ResponEntity.Code_Autho_Fail){
                    login(nil);
                    //然后重新运行下 监听下登录情况
                    getMyCenter(preTaskSerialNumber:taskSerialNumber ,end: end);
                }else{
                    end(re);
                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }
    @discardableResult
    class func getUserProtocol(preTaskSerialNumber : Int?,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    getUserProtocol(preTaskSerialNumber:taskSerialNumber ,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }
        let request = Alamofire
            .request(getAPi(), method: .post,
                     parameters:[
                        "action": "regcons"
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:false ){ (data :JSON)->Any in
                    return ArticleDetailTo.init(jsons: data.dictionary);
                }
                
                //状态逻辑处理
                //重新登录
                if(re.res==ResponEntity.Code_Autho_Fail){
                    login(nil);
                    //然后重新运行下 监听下登录情况
                    getUserProtocol(preTaskSerialNumber:taskSerialNumber ,end: end);
                }else{
                    end(re);
                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }

    @discardableResult
    class func getArticleDetail(preTaskSerialNumber : Int?,aid:String,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    getArticleDetail(preTaskSerialNumber:taskSerialNumber ,aid:aid,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }
        let request = Alamofire
            .request(getAPi(), method: .post,
                     parameters:[
                        "action": "articleInfo",
                        "aid":aid
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:false ){ (data :JSON)->Any in
                    return ArticleDetailTo.init(jsons: data.dictionary);
                }
                
                //状态逻辑处理
                //重新登录
                if(re.res==ResponEntity.Code_Autho_Fail){
                    login(nil);
                    //然后重新运行下 监听下登录情况
                    getArticleDetail(preTaskSerialNumber:taskSerialNumber ,aid:aid,end: end);
                }else{
                    end(re);
                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }
    @discardableResult
    class func getSuppliers(preTaskSerialNumber : Int?,page:Int,mainId:String,classId:String,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    getSuppliers(preTaskSerialNumber:taskSerialNumber ,page:page,mainId:mainId,classId:classId,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }
        //http://120.77.8.217:1024/Tools/Api/api.ashx?action=page_prolist
        let request = Alamofire
            .request(getAPi(), method: .post,
                     parameters:[
                        "action": "page_articlelist_sup",
                        "currentpage":page,
                        "pcid":mainId,
                        "cid":classId//1就是新闻得
                        
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:false ){ (data :JSON)->Any in
                    return ArticlesTo.init(jsons: data.dictionary);
                }
                
                //状态逻辑处理
                //重新登录
                if(re.res==ResponEntity.Code_Autho_Fail){
                    login(nil);
                    //然后重新运行下 监听下登录情况
                    getSuppliers(preTaskSerialNumber:taskSerialNumber ,page:page,mainId:mainId,classId:classId,end: end);
                }else{
                    end(re);
                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }

    
    @discardableResult
    class func getArticles(preTaskSerialNumber : Int?,page:Int,mainId:String,classId:String,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    getArticles(preTaskSerialNumber:taskSerialNumber ,page:page,mainId:mainId,classId:classId,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }
        //http://120.77.8.217:1024/Tools/Api/api.ashx?action=page_prolist
        let request = Alamofire
            .request(getAPi(), method: .post,
                     parameters:[
                        "action": "page_articlelist",
                        "currentpage":page,
                        "pcid":mainId,
                        "cid":classId//1就是新闻得
                        
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:false ){ (data :JSON)->Any in
                    return ArticlesTo.init(jsons: data.dictionary);
                }
                
                //状态逻辑处理
                //重新登录
                if(re.res==ResponEntity.Code_Autho_Fail){
                    login(nil);
                    //然后重新运行下 监听下登录情况
                    getArticles(preTaskSerialNumber:taskSerialNumber ,page:page,mainId:mainId,classId:classId,end: end);
                }else{
                    end(re);
                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }
    
    /**
     * 产品详情 里面的 图片列表
     * @param c
     * @param productId
     * @param cid 类别Id
     * @param sa
     * @return
     */
@discardableResult
    class func getProductImages(preTaskSerialNumber : Int?, productId :String, cid :String,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    getProductImages(preTaskSerialNumber:taskSerialNumber ,productId:productId,cid:cid,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }
        //http://120.77.8.217:1024/Tools/Api/api.ashx?action=propic&cid=644&pid=182
        let request = Alamofire
            .request(getAPiPwd(), method: .post,
                     parameters:[
                        "c":encryptOfUser([
                            "action": "propic",
                            "pid":productId,
                            "cid":cid
                        ])
                        
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
 
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:false ){ (data :JSON)->Any in
                    return ProductDetailImageTypeItemTo.initToList(json:data);
                }
                
                //状态逻辑处理
                //重新登录
                if(re.res==ResponEntity.Code_Autho_Fail){
                    login(nil);
                    //然后重新运行下 监听下登录情况
                    getProductImages(preTaskSerialNumber:taskSerialNumber ,productId:productId,cid:cid,end: end);
                }else{
                    end(re);
                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }
    class func getProductlDetailAPI(id:String)->String{
    
        return getAPi()+"?action=page_proinfo&type=1&pid=\(id)";
        
    }
    @discardableResult
    class func getProductlDetail(preTaskSerialNumber : Int?,id:String,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    getProductlDetail(preTaskSerialNumber:taskSerialNumber ,id:id,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }

       let request = Alamofire
            .request(getAPi(), method: .post,
                     parameters:[
                        "action": "page_proinfo",
                        "type":"1",
                        "pid":id
                ])
        .responseJSON { (response) in
            RequestIng.removeValue(forKey: LoginTaskSerialNum)
            //加载失败
            guard response.result.isSuccess || (response.result.value != nil) else {
                end( ResponEntity.netFail());
                return
            }
            
            //解析数据
            let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:false ){ (data :JSON)->Any in
                return ProductDetailTo.init(jsons: data.dictionary);
            }
            
            //状态逻辑处理
            //重新登录
            if(re.res==ResponEntity.Code_Autho_Fail){
                login(nil);
                //然后重新运行下 监听下登录情况
                getProductlDetail(preTaskSerialNumber:taskSerialNumber ,id:id,end: end);
            }else{
                end(re);
            }
        }//responseJSON end


        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }

    
    /**
     preTaskSerialNumber 外部调用直接nil
     */
    @discardableResult
    class func getProductls(preTaskSerialNumber : Int?,page:Int ,mainId:String,filterIds:String,keywords:String,end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        if let pts = preTaskSerialNumber{
            taskSerialNumber=pts;
        }else{
            taskSerialNumber=NextTaskSerialNumber();
        }
        
        if isLoging {
            LoginAfterRun[taskSerialNumber] = LoginAfterRunType{ isSuccess in
                if(isSuccess){
                    getProductls(preTaskSerialNumber:taskSerialNumber ,page:page,mainId:mainId,filterIds:filterIds,keywords:keywords,end: end);
                }else{
                    end(ResponEntity.Fail());
                }
            }
            return taskSerialNumber;
        }
        //http://120.77.8.217:1024/Tools/Api/api.ashx?action=page_prolist
        let request = Alamofire
            .request(getAPi(), method: .post,
                     parameters:[
                        "action": "page_prolist",
                        "cid":mainId,
//                        "kid_7":"",//所有户型
//                        "kid_8":"",//所有风格
                        "filterIds":filterIds,
                        "keywords":keywords,
                        "currentpage":page,
                        "pagesize":String(LKMAPIListPageSize)

                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
              
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:false ){ (data :JSON)->Any in
                    return ProductsTo.init(jsons: data.dictionary);
                }
                
                //状态逻辑处理
                //重新登录
                if(re.res==ResponEntity.Code_Autho_Fail){
                    login(nil);
                    //然后重新运行下 监听下登录情况
                    getProductls(preTaskSerialNumber:taskSerialNumber,page:page,mainId: mainId,filterIds:filterIds,keywords:keywords,end:end);
                }else{
                    end(re);
                }
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }
    
    //是否在登录中
    static var isLoging=false;
    static var LoginTaskSerialNum=NilTaskSerialNumber;//当前登录的，因为只会有一个界面在监听
    class func login(isRetry:Bool=true,_ end: ( (ResponEntity)->())? ){
        let us=UserInfoDao.getUserAccount();
        
        login(username:us.username,
              password:us.userpwd,
              isRetry:isRetry,
              end)
    }
    class func loginGuest(_ end: ( (ResponEntity)->())? ){
        login(username: "游客登录abc", password: "游客登录abc", end)
    }
    
    //(@escaping () -> Swift.Void)? = nil
    class func login(username:String,password:String, isRetry:Bool=true,_ end: ( (ResponEntity)->())? ){
        if(isLoging){
            return;
        }
        if(username.isEmpty||password.isEmpty){
            end?(ResponEntity.Fail())
        }
        
        isLoging=true;
        //避免重复生成序列码
        if(LoginTaskSerialNum==NilTaskSerialNumber){
            LoginTaskSerialNum=NextTaskSerialNumber();
        }
        
        UserInfoCache.getInstance().clearUserInfo();
        
        if("游客登录abc" == username){//游客登录
            let re = ResponEntity(dict: JSON(data: StringHelp.toData( "{\"res\":1,\"msg\":\"获取成功\",\"data\":{\"userid\":\"0\",\"realname\":\"游客\",\"nickname\":\"游客\",\"pwd_sign\":\"xurongting666rob\",\"vip_now\":\"1\",\"times\":1476075681978}}" )! ),isDataNotNil: true){ (dict :JSON)->Any  in
                return LoginTo.init(jsons: dict.dictionary);
            }
            
            loginAfter(re: re, end)
            return
        }
        //        "username",telephone);
        //        "password",password);
        //        "action","login");
        //        "mobileinfo",UTDevice.getUtdid(c));
        let encryptContent=encrypt(["username":username,
                                    "password":password,
                                    "action":"login",
                                    "mobileinfo":OsRuntimeEnvInfo.getUUID() ])
        //        OsRuntimeEnvInfo.getUUID()
        
        let loadBeforTime = ServerTimeSync.currentTimeMillis();
        let request = Alamofire
            .request(getAPiPwd(), method: .post,parameters:[
                "c":encryptContent
                ],headers:["Cookie":""])
            .responseJSON { (response) in
                UserInfoDao.saveUser(name: username, pass: password)

                
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                
                let loadAfterTime = ServerTimeSync.currentTimeMillis();
                
                guard response.result.isSuccess || (response.result.value != nil) else {
                   // end?( ResponEntity.netFail());
                    loginAfter(re: ResponEntity.netFail(), end);
                    return
                }
                
                let value = response.result.value;
                let re = ResponEntity(dict: JSON(value),isDataNotNil: true){ (dict :JSON)->Any  in
                    return LoginTo.init(jsons: dict.dictionary);
                }
                LKMLog("======\(value)")
                //时间
                if let datap = re.data{
                    
                    let mloginTo=datap as! LoginTo ;
                    if let times = mloginTo.times{
                        ServerTimeSync.getInstance().setNetLoadServerTime(loadDuration: loadAfterTime-loadBeforTime,localTime: loadAfterTime,serverTime:  times );
                    }
                }
                
                //时间失败，重新登录
                if(re.res==ResponEntity.Code_Login_time_fail&&isRetry){
                    isLoging=false;
                    login(username:username,password:username,isRetry:false,end);//不重复登录了
                    //重新登录 不重置taskser
                    return;
                }else{
                    loginAfter(re:re,end)
                }
                
                
        }
        
        RequestIng[LoginTaskSerialNum]=request;
        
        
    }//login end
    
    //登录后
    private class func loginAfter( re:ResponEntity, _ end: ( (ResponEntity)->())?){
        isLoging=false;
        end?(re);
        
        //修改记得看注册
        let isSuccess=re.isSuccess();
        if(isSuccess){
            UserInfoCache.getInstance().loginData(loginTo: re.data as! LoginTo)
            Api.post_xgtoken(preTaskSerialNumber: nil, deviceId: CloudPushSDK.getDeviceId(), end: { (re:ResponEntity) in
                //先不鸟任何事
            })
        }
        
        //登录完成，或是失败。
        for itemRun in LoginAfterRun.values {
            itemRun.run(isSuccess);
        }
        LoginAfterRun.removeAll();
        
        //这次登录完成
        LoginTaskSerialNum=Api.NilTaskSerialNumber
    }
    
    class func PassAlter(telephone:String,password:String, mobile_valicode:String, end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        taskSerialNumber=NextTaskSerialNumber();
        let request = Alamofire
            .request(getAPiPwd(), method: .post,
                     parameters:[
                        "c":encrypt([
                            "passcode":mobile_valicode,
                            "tel":telephone,
                            "password":password,
                            "action":"pwdchange",
                            ])
                        
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:true ){ (data : JSON) in
                    return LoginTo.init(jsons: data.dictionary );
                }
                end(re);
                
        }//responseJSON end
        FXM.D("修改密码，不应该跟登录一样吧？去掉cookie让它自动登录吧？")
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }

    class func reg(telephone:String,password:String, mobile_valicode:String, end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        taskSerialNumber=NextTaskSerialNumber();
       let request = Alamofire
            .request(getAPiPwd(), method: .post,
                     parameters:[
                        "c":encrypt([
                            "passcode":mobile_valicode,
                            "tel":telephone,
                            "password":password,
                            "mobileinfo": OsRuntimeEnvInfo.getUUID(),
                            "action":"reguser"
                            ])
                        
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                
//                String(data: <#T##Data#>, encoding: <#T##String.Encoding#>)
                LKMLog(String(data:response.data!,encoding:String.Encoding.utf8)!)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:true ){ (data : JSON) in
                    return LoginTo.init(jsons: data.dictionary );
                }
                
                //登录那边记得看下  一样的
                if(re.isSuccess()){
                    UserInfoDao.saveUser(name: telephone, pass: password)
                    UserInfoCache.getInstance().loginData(loginTo: re.data as! LoginTo)
                }
                
                end(re);
                
        }//responseJSON end
        
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }
    class func sendSmsAuthForFindPass(telephone:String, end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        taskSerialNumber=NextTaskSerialNumber();
        let request = Alamofire
            .request(getAPiPwd(), method: .post,
                     parameters:[
                        "c":encrypt([
                            "action": "getpwdcode",
                            "tel":telephone
                            ])
                        
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:false ){_ in
                    return "";
                }
                end(re);
                
        }//responseJSON end
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }

    /**
     preTaskSerialNumber 外部调用直接nil
     */
    class func sendSmsAuthForReg(telephone:String, end: @escaping (ResponEntity)->() )->Int{
        var taskSerialNumber :Int = 0;
        taskSerialNumber=NextTaskSerialNumber();
        let request = Alamofire
            .request(getAPiPwd(), method: .post,
                     parameters:[
                        "c":encrypt([
                            "action": "getregcode",
                            "tel":telephone
                        ])
                        
                ])
            .responseJSON { (response) in
                RequestIng.removeValue(forKey: LoginTaskSerialNum)
                //加载失败
                guard response.result.isSuccess || (response.result.value != nil) else {
                    end( ResponEntity.netFail());
                    return
                }
                //解析数据
                let re = ResponEntity(dict: JSON(response.result.value),isDataNotNil:false ){_ in
                    return "";
                }
                end(re); 
               
        }//responseJSON end
        
        RequestIng[LoginTaskSerialNum]=request;
        return taskSerialNumber;
    }

    
//    static let mOperationQueue:OperationQueue=OperationQueue();
//    func test(){
//        Task<String,String,NSError>{ progress, fulfill, reject, configure in
//            
//            let mopt = myOperationThread()
//            mOperationQueue.addOperation(mopt)
//            
//            configure.cancel={
//                mopt.cancel();
//            }
//            
//            configure.pause={
//                mOperationQueue.isSuspended=true;
//                
//                //                mopt.cancel();
//            }
//            configure.resume={
//                mOperationQueue.isSuspended=false;
//            }
//            
//        }
//        
//        
//
//    }
//    var queue = OperationQueue()
//        func testNSOperationQueue()
//    {
//        //func (op: NSOperation!)
//        let mopt = myOperationThread()
//        queue.addOperation(mopt)
//        //        mopt.cancel();
//    }
//    
//    
//    
//    class myOperationThread : Operation
//    {
//        
//        override func main()
//        {
//            //        for var i = 0; i < 100000; i++
//            //        {
//            print("NSOperation running.")
//            //        }
//            print("NSOperation over.")
//        }
//    }
}
